package com.jsmtr.rms.service.EasyShuttleService.impl;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.BaseUser;
import com.jsmtr.rms.entity.DUserResume;
import com.jsmtr.rms.entity.DoZysx;
import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.service.EasyShuttleService.IEasyShuttleService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 一键操作服务类
 * @author coffee
 * @date 2020/7/1 15:32
 */
@Service
public class EasyShuttleService implements IEasyShuttleService {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private DaoService daoService;

    @Autowired
    private IUtilService utilService;

    public IProjectService getProjectService() {
        return projectService;
    }

    public void setProjectService(IProjectService projectService) {
        this.projectService = projectService;
    }

    public DaoService getDaoService() {
        return daoService;
    }

    public void setDaoService(DaoService daoService) {
        this.daoService = daoService;
    }

    public IUtilService getUtilService() {
        return utilService;
    }

    public void setUtilService(IUtilService utilService) {
        this.utilService = utilService;
    }

    @Override
    public List<Map> StatusStatistics(String pid, String fid, String jid) throws Exception {
        String jlzt=createStateColum(fid);
        String fbzt=createOpColum(fid);
        String sql=" select d."+jlzt+" as jlzt,d."+fbzt+" as fbzt,count(1)  from VIEW_DUSERRESUME d where d.proid=:pid and  d.fid=:fid and d.jid=:jid group by d."+jlzt+",d."+fbzt+",d.jid";
        Map params=new HashMap();
        params.put("pid",pid);
        params.put("fid",fid);
        params.put("jid",jid);
        return daoService.executeSQLQueryToMap(sql,params);
    }

    /**
     * 得到发布列名
     * @param fid
     * @return
     */
    private String createOpColum(String fid){
        ProjectInfoFlow projectInfoFlow;
        String opColumns = "";
        try {
            projectInfoFlow = projectService.getFolwById(fid);
            if (projectInfoFlow != null) {
                if ("简历筛选".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op1";
                }else if ("笔试".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op6";
                }else if("资料审核".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op2";
                }else if("面试安排".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op7";
                }else if ("背景调查".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op3";
                }else if ("综合考核".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op10";
                }else if("体检".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op4";
                }else if("签约".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op9";
                }else if("录用".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op5";
                }else if("实习".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op7";
                }else if("实作".equalsIgnoreCase(projectInfoFlow.getFname())){
                    opColumns="op8";
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return opColumns;
    }

    /**
     * 得到流程状态列明
     * @param fid
     * @return
     */
    private String createStateColum(String fid){
        ProjectInfoFlow projectInfoFlow;
        String stateColumns="";
        try {
            projectInfoFlow = projectService.getFolwById(fid);
            if (projectInfoFlow != null) {
                if ("简历筛选".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="state";
                }else if ("笔试".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="bsstate";
                }else if("资料审核".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="zlstate";
                }else if("面试安排".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="msstate";
                }else if ("背景调查".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="zsstate";
                }else if ("综合考核".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="zhstate";
                }else if("体检".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="tjstate";
                }else if("签约".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="qystate";
                }else if("录用".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="bdstate";
                }else if("实习".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="sxstate";
                }else if("实作".equalsIgnoreCase(projectInfoFlow.getFname())){
                    stateColumns="opstate";
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return stateColumns;
    }

    @Override
    public List<Map> StatusStatistics(String pid, String fid) throws Exception {

        return null;
    }

    @Override
    public List<Map> StatusStatistics(String pid) {
        return null;
    }

    @Override
    public Integer EasyPublic(String pid, String fid, String jid) throws Exception {
        String sql="";
        ProjectInfoFlow projectInfoFlow=projectService.getFolwById(fid);
        Integer result=0;
        if (projectInfoFlow!=null) {
            if ("笔试".equalsIgnoreCase(projectInfoFlow.getFname())) {
                sql=" update  dobs_user u set u.op6='已公布' where u.fid=:fid and u.userid in (select d.id from duserresume d where d.priod=:pid and d.jid=:jid and d.path like :fid )";
            } else if ("面试".equalsIgnoreCase(projectInfoFlow.getFname())) {
                sql=" update  doms_user u set u.op7='已公布' where u.fid=:fid and u.userid in (select d.id from duserresume d where d.priod=:pid and d.jid=:jid and d.path like :fid )";
            } else if ("实操".equalsIgnoreCase(projectInfoFlow.getFname())) {
                sql=" update  dooperation_user u set u.op8='已公布' where u.fid and u.userid in (select d.id from duserresume d where d.priod=:pid and d.jid=:jid and d.path like :fid )";
            }else{
                sql= " update duserresume u set u."+createOpColum(fid)+"='已公布' where  u.proid=:pid and u.jid=:jid and d.path like :fid ";
            }
            Map params=new HashMap();
            params.put("pid",pid);
            params.put("fid",fid);
            params.put("jid",jid);
            result= daoService.executeSQL(sql,params);
        }
        return result;
    }

    @Override
    public Integer EasyPublic(String pid, String fid) throws Exception {
        String sql="";
        ProjectInfoFlow projectInfoFlow=projectService.getFolwById(fid);
        Integer result=0;
        if (projectInfoFlow!=null) {
            if ("笔试".equalsIgnoreCase(projectInfoFlow.getFname())) {
                sql=" update  dobs_user u set u.op6='已公布' where u.fid=:fid and u.userid in (select d.id from duserresume d where d.priod=:pid  and d.path like :fid )";
            } else if ("面试".equalsIgnoreCase(projectInfoFlow.getFname())) {
                sql=" update  doms_user u set u.op7='已公布' where u.fid=:fid and u.userid in (select d.id from duserresume d where d.priod=:pid  and d.path like :fid )";
            } else if ("实操".equalsIgnoreCase(projectInfoFlow.getFname())) {
                sql=" update  dooperation_user u set u.op8='已公布' where u.fid and u.userid in (select d.id from duserresume d where d.priod=:pid  and d.path like :fid )";
            }else{
                sql= " update duserresume u set u."+createOpColum(fid)+"='已公布' where  u.proid=:pid  and d.path like :fid ";
            }
            Map params=new HashMap();
            params.put("pid",pid);
            params.put("fid","'%"+fid+"%'");
            result= daoService.executeSQL(sql,params);
        }
        return result;
    }


    @Override
    public Integer EasyMsg(String msg, String pid, String fid, String jid) {
        return null;
    }

    @Override
    public Integer EasyMsg(String msg, String pid, String fid) {
        return null;
    }

    @Override
    public Integer EasyMsg(String msg, String pid) {
        return null;
    }

    @Override
    public Integer EasyNews(String news, String pid, String fid, String jid) {
        return null;
    }

    @Override
    public Integer EasyNews(String news, String pid, String fid) {
        return null;
    }

    @Override
    public Integer EasyNews(String news, String pid) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public Integer EasyAssignment(String pid, String fid,String jname,Map infos,int count,String checkColumn) throws Exception {
        String sql="";
        ProjectInfoFlow projectInfoFlow=projectService.getFolwById(fid);
        Integer result=0;
        if (projectInfoFlow!=null) {
            if ("资料审核".equalsIgnoreCase(projectInfoFlow.getFname())||"体检".equalsIgnoreCase(projectInfoFlow.getFname())||"录用".equalsIgnoreCase(projectInfoFlow.getFname())) {
                String upInfos=createAssignmentInfo(infos,"u");
                if (StringTools.trimNotEmpty(upInfos)) {
                    if (pid.toUpperCase().startsWith("C")){
                        sql = " update hr_zzuser u set "+upInfos+
                                " where  u.pid=:pid  and u.path like :fid and u.job=:jname and u.id  in (select id from (select d.id,ROWNUM from hr_zzuser d where d." + checkColumn + " is null and ROWNUM<="+count+" and d.path like :fid and  d.job=:jname)) ";
                    }else{
                        sql = " update duserresume u set "+upInfos+
                                " where  u.proid=:pid  and u.path like :fid and u.jname=:jname and u.id  in (select id from (select d.id,ROWNUM from duserresume d where d." + checkColumn + " is null and ROWNUM<="+count+" and d.path like :fid and  d.jname=:jname)) ";
                    }
                }
                Map params=new HashMap();
                params.put("pid",pid);
                params.put("fid","%"+fid+"%");
                params.put("jname",jname);
                result= daoService.executeSQL(sql,params);
            }
        }
        return result;
    }

    public void EasyZysx(String pid, String fid,  String jname,int count,String checkColumn, BaseUser baseUser,String zysxinfo) throws Exception{
        String sql="";
        ProjectInfoFlow projectInfoFlow=projectService.getFolwById(fid);
        String sxtype="";
        if("资料审核".equalsIgnoreCase(projectInfoFlow.getFname())){
            DUserResume dUserResume=new DUserResume();
            dUserResume.setProid(pid);
            dUserResume.setPath(fid);
            sxtype="ZL";
        }else if("面试".equalsIgnoreCase(projectInfoFlow.getFname())||"面试安排".equalsIgnoreCase(projectInfoFlow.getFname())){
            sxtype="MS";
        }else if("体检".equalsIgnoreCase(projectInfoFlow.getFname())){
            sxtype="TJ";
        }else if("录用".equalsIgnoreCase(projectInfoFlow.getFname())){
            sxtype="LY";
        }
        String  findSql;
        if (pid.toUpperCase().indexOf("C_")>=0) {
            findSql = " select u.id from hr_zzuser u where u.proid='" + pid + "' and u.job='" + jname + "' and u.path like '%" + fid + "%' and u." + checkColumn + " is null ";
        }else {
            findSql =" select u.id from duserresume u where u.proid='" + pid + "' and u.jname='" + jname + "' and u.path like '%" + fid + "%' and u." + checkColumn + " is null ";
        }
        List<Map<String, Object>> duseresumeIdList= daoService.executeSQLQueryMapPage(findSql,null,0,count);
        ArrayList addlist=new ArrayList();
        ArrayList editlist=new ArrayList();
        if (duseresumeIdList!=null&&duseresumeIdList.size()>0){
            for (Map<String, Object> stringObjectMap : duseresumeIdList) {
                String uid=stringObjectMap.get("ID").toString();
                if(zysxinfo!=null&&zysxinfo.trim().length()>0){
                    DoZysx userZysx= utilService.getUserZysx(pid,fid,uid);
                    DoZysx zysx=null;
                    if (userZysx==null){
                        zysx=new DoZysx();
                        zysx.setId(UUID.randomUUID().toString());
                        zysx.setFid(fid);
                        zysx.setPid(pid);
                        zysx.setRemark(zysxinfo);
                        zysx.setUserid(uid);
                        zysx.setType(sxtype);
                        zysx.setAddtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                        zysx.setAdduser(baseUser.getUcode());
                        addlist.add(zysx);
                    }else{
                        zysx=userZysx;
                        zysx.setRemark(zysxinfo);
                        zysx.setType(sxtype);
                        zysx.setAddtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                        zysx.setAdduser(baseUser.getUcode());
                        editlist.add(zysx);
                    }

                }
            }
            if (editlist!=null&&editlist.size()>0){
                utilService.editList(editlist);
            }else if(addlist!=null&&addlist.size()>0){
                utilService.addList(addlist);
            }

        }


    }

    private String createAssignmentInfo(Map params,String tname){
        String sets="";
        if (params!=null){
            for (Object o : params.keySet()) {
                if(StringTools.trimNotEmpty(params.get(o))){
                    sets+=tname+"."+o.toString().toLowerCase()+"='"+params.get(o).toString()+"',";
                }
            }
            sets=sets.substring(0,sets.length()-1);

        }
        return sets;
    }


}
